//
//  MoreSortOrderController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/12/17.
//  Copyright © 2017 蓝布鲁. All rights reserved.
//

#import "MoreSortOrderController.h"
#import "View+MASAdditions.h"
#import "TeacherModel.h"
#import "TeacherCell.h"
#import "TeacherInfoViewController.h"

@interface MoreSortOrderController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)NSMutableArray *dataArray;

@property (nonatomic, assign)NSInteger page;

@end

@implementation MoreSortOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"查询结果";

    // 1.初始化tableView
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];

    // 2.获取网络数据

    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 请求导师数据
        self.page = 0;
        [self updateTeacherDateWithPage:self.page getCount:12];
    }];

    [self.tableView.mj_header beginRefreshing];

    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 获取导师数据
        self.page++;
        [self updateTeacherDateWithPage:self.page getCount:12];
    }];
}

// 请求老师信息
- (void)updateTeacherDateWithPage:(NSInteger)page getCount:(NSInteger)count {
    // 判断是否(下拉刷新)
    if (page == 0) {
        // 下拉刷新,则清空数组
        self.dataArray = [[NSMutableArray alloc] init];
    }

    // 初始化请求参数
    NSDictionary *parameters = @{ @"startTime": self.startTimeString, @"endTime": self.endTimeString,@"page": @(page), @"count": @(count)};


    [[NetworkTool shareInstance] requireMethodType:POSTType URLString:@"SDSY/getTeacherInTime" parameters:parameters success:^(NSDictionary *respondDictionary) {
        // 1.储存数据
        for (NSDictionary *dic in respondDictionary[@"Teachers"]) {
            TeacherModel *model = [[TeacherModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];

        // 2.检查是否正常加载
        if ([respondDictionary[@"Teachers"] count] < count) {
            // 没有更多数据
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            // 正常结束刷新
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];

    } failure:^(NSError *error) {

        [self updateFailed];
    }];

}

// 如果请求失败,HUD显示错误,tableVIew刷新空数据,foot提示没有数据
- (void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}

#pragma mark UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TeacherCell *teacherCell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (teacherCell == nil) {
        teacherCell = [[TeacherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];

    }
    // 取出模型
    TeacherModel *model = self.dataArray[indexPath.row];

    // 初始化cell数据
    teacherCell.model = model;
    return teacherCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView cellHeightForIndexPath:indexPath model:self.dataArray[indexPath.row] keyPath:@"model" cellClass:[TeacherCell class] contentViewWidth:SCREEN_WIDTH];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];

    // 取出cell
    TeacherCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 弹出导师详情
    TeacherInfoViewController *teacherInfoViewController = [[TeacherInfoViewController alloc] init];
    teacherInfoViewController.teacherID = cell.model.tea_id;
    [self.navigationController pushViewController:teacherInfoViewController animated:true];
}


@end
